package com.wtwd.campus.controller;

import com.alibaba.fastjson.JSON;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespCodeEntity;
import com.wtwd.campus.config.ProducerConfig;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.utils.ByteObjectUtils;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author ldaoliang
 * @Date create in 15:01 2021/2/21
 * @Description
 */
@RestController
@RequestMapping("/command")
public class SendCommandController {

    private static final Logger logger = LoggerFactory.getLogger(SendCommandController.class);
    @Autowired
    private DefaultMQProducer producer;
    @Autowired
    private ProducerConfig producerConfig;

    /**
     * 主动查询定位
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/queryDeviceLocation/{imei}/{commandNumber}")
    public RespCodeEntity queryDeviceLocation(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralQuery mqGeneralQuery = new MqGeneralQuery();
        mqGeneralQuery.setCommandNumber(commandNumber);
        mqGeneralQuery.setImei(imei);
        byte[] messages = getMqMessage(Constants.ACTIVE_DEVICE_LOCATION, mqGeneralQuery);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }


    /**
     * 查询设备参数
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/queryDeviceParam/{imei}/{commandNumber}")
    public RespCodeEntity queryDeviceParam(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralQuery mqGeneralQuery = new MqGeneralQuery();
        mqGeneralQuery.setCommandNumber(commandNumber);
        mqGeneralQuery.setImei(imei);
        byte[] messages = getMqMessage(Constants.DEVICE_PARAMETERS, mqGeneralQuery);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 查询设备Iccid和SimCard号
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/queryDeviceIccidAndSimCardNo/{imei}/{commandNumber}")
    public RespCodeEntity queryDeviceIccidAndSimCardNo(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralQuery mqGeneralQuery = new MqGeneralQuery();
        mqGeneralQuery.setCommandNumber(commandNumber);
        mqGeneralQuery.setImei(imei);
        byte[] messages = getMqMessage(Constants.ICCID_AND_SIM, mqGeneralQuery);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 查询设备Imeid
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/queryDeviceImeid/{imei}/{commandNumber}")
    public RespCodeEntity queryDeviceImeid(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralQuery mqGeneralQuery = new MqGeneralQuery();
        mqGeneralQuery.setCommandNumber(commandNumber);
        mqGeneralQuery.setImei(imei);
        byte[] messages = getMqMessage(Constants.DEVICE_IMEID, mqGeneralQuery);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置临时服务器地址
     *
     * @param mqSetIpAndPort
     * @return
     */
    @PostMapping("/setTempIpAndPort")
    RespCodeEntity setTempIpAndPort(@RequestBody MqSetIpAndPort mqSetIpAndPort) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] messages = getMqMessage(Constants.SET_TEMPORARY_CONTROL_CENTER, mqSetIpAndPort);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置默认服务器地址
     *
     * @param mqSetIpAndPort
     * @return
     */
    @PostMapping("/setDefaultIpAndPort")
    RespCodeEntity setDefaultIpAndPort(@RequestBody MqSetIpAndPort mqSetIpAndPort) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] messages = getMqMessage(Constants.SET_DEFAULT_CONTROL_CENTER, mqSetIpAndPort);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置工作模式
     *
     * @param mqSetWorkMode
     * @return
     */
    @PostMapping("/setWorkMode")
    RespCodeEntity setWorkMode(@RequestBody MqSetWorkMode mqSetWorkMode) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] messages = getMqMessage(Constants.SET_WORK_MODE, mqSetWorkMode);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置上报间隔
     *
     * @param imei
     * @param commandNumber
     * @param interval
     * @return
     */
    @PostMapping("/setUpLoadInterval/{imei}/{commandNumber}/{interval}")
    RespCodeEntity setUpLoadInterval(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("interval") Integer interval) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setValue(interval.toString());
        byte[] messages = getMqMessage(Constants.SET_UPLOAD_TIME, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置监听号码
     *
     * @param imei
     * @param commandNumber
     * @param phone
     * @return
     */
    @PostMapping("/setMonitorPHone/{imei}/{commandNumber}/{phone}")
    RespCodeEntity setMonitorPhone(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("phone") String phone) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setValue(phone);
        byte[] messages = getMqMessage(Constants.SET_MONITOR_NUMBER, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置闲时自动切换模式
     *
     * @param mqSleepTimeAutoChange
     * @return
     */
    @PostMapping("/setSleepTimeAutoChangeMode")
    RespCodeEntity setSleepTimeAutoChangeMode(@RequestBody MqSleepTimeAutoChange mqSleepTimeAutoChange) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] messages = getMqMessage(Constants.AUTO_CHANGE_WORK_MODE, mqSleepTimeAutoChange);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置课堂模式（免打扰协议）
     *
     * @param deviceClassMode
     * @return
     */
    @PostMapping("/setClassMode")
    RespCodeEntity setClassMode(@RequestBody DeviceClassMode deviceClassMode) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] messages = getMqMessage(Constants.SET_NO_DISTURB_TIME, deviceClassMode);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置免打扰协议开关
     *
     * @param imei
     * @param commandNumber
     * @param flag
     * @return
     */
    @PostMapping("/setClassModeSwitch/{imei}/{commandNumber}/{flag}")
    RespCodeEntity setClassModeSwitch(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("flag") Integer flag) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSwitch mqGeneralSwitch = new MqGeneralSwitch();
        mqGeneralSwitch.setCommandNumber(commandNumber);
        mqGeneralSwitch.setImei(imei);
        mqGeneralSwitch.setOnOff(flag);
        byte[] messages = getMqMessage(Constants.SET_NO_DISTURB_SWITCH, mqGeneralSwitch);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置PDOP门限
     *
     * @param imei
     * @param commandNumber
     * @param thresholdParam
     * @return
     */
    @PostMapping("/setPDOP/{imei}/{commandNumber}/{thresholdParam}")
    RespCodeEntity setPDOP(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("thresholdParam") Integer thresholdParam) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setValue(thresholdParam.toString());
        byte[] messages = getMqMessage(Constants.SET_PDOP_THRESHOLD_VALUE, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置SOS开关
     *
     * @param imei
     * @param commandNumber
     * @param onOff
     * @return
     */
    @PostMapping("/setSOSSwitch/{imei}/{commandNumber}/{onOff}")
    RespCodeEntity setSOSSwitch(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("onOff") Integer onOff) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setValue(onOff.toString());
        byte[] messages = getMqMessage(Constants.SET_SOS_DIAL_SWITCH, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 恢复出厂设置
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/reSetDevice/{imei}/{commandNumber}")
    RespCodeEntity reSetDevice(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSysSet mqGeneralSysSet = new MqGeneralSysSet();
        mqGeneralSysSet.setCommandNumber(commandNumber);
        mqGeneralSysSet.setImei(imei);
        byte[] messages = getMqMessage(Constants.FACTORY_RESET, mqGeneralSysSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 重启设备
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/reStartDevice/{imei}/{commandNumber}")
    RespCodeEntity reStartDevice(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSysSet mqGeneralSysSet = new MqGeneralSysSet();
        mqGeneralSysSet.setCommandNumber(commandNumber);
        mqGeneralSysSet.setImei(imei);
        byte[] messages = getMqMessage(Constants.DEVICE_RESTART, mqGeneralSysSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设备关机
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/shutDownDevice/{imei}/{commandNumber}")
    RespCodeEntity shutDownDevice(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSysSet mqGeneralSysSet = new MqGeneralSysSet();
        mqGeneralSysSet.setCommandNumber(commandNumber);
        mqGeneralSysSet.setImei(imei);
        byte[] messages = getMqMessage(Constants.SOFTWARE_TURN_OFF, mqGeneralSysSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 音量调节
     *
     * @param imei
     * @param commandNumber
     * @param speakLevel
     * @return
     */
    @PostMapping("/adjustVolume/{imei}/{commandNumber}/{speakLevel}")
    RespCodeEntity adjustVolume(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("speakLevel") Integer speakLevel) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setValue(speakLevel.toString());
        byte[] messages = getMqMessage(Constants.VOLUME_ADJUSTMENT, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 切换设备id
     *
     * @param imei
     * @param commandNumber
     * @param idParam
     * @return
     */
    @PostMapping("/exchangeDeviceId/{imei}/{commandNumber}/{idParam}")
    RespCodeEntity exchangeDeviceId(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("idParam") Integer idParam) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setValue(idParam.toString());
        byte[] messages = getMqMessage(Constants.CHANGE_DEVICE_ID, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置白名单
     *
     * @param deviceWhiteCall
     * @return
     */
    @PostMapping("/setWhiteCall")
    RespCodeEntity setWhiteCall(@RequestBody DeviceWhiteCall deviceWhiteCall) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] messages = getMqMessage(Constants.SINGLE_WHITELIST_NUMBER, deviceWhiteCall);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 一次性设置所有白名单
     *
     * @param mqSetAllWhiteCall
     * @return
     */
    @PostMapping("/setAllWhiteCall")
    RespCodeEntity setAllWhiteCall(@RequestBody MqSetAllWhiteCall mqSetAllWhiteCall) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] messages = getMqMessage(Constants.ALL_WHITELIST_NUMBER, mqSetAllWhiteCall);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 一次性设置所有sos号码
     */
    @PostMapping("/setAllSosCall")
    RespCodeEntity setAllSosCall(@RequestBody MqSetAllSosCall mqSetAllSosCall) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] messages = getMqMessage(Constants.ALL_SOS_PHONE, mqSetAllSosCall);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }


    /**
     * 设置白名单开关
     *
     * @param imei
     * @param commandNumber
     * @param onOff
     * @return
     */
    @PostMapping("/setWhiteCallSwitch/{imei}/{commandNumber}/{onOff}")
    RespCodeEntity setWhiteCallSwitch(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("onOff") Integer onOff) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSwitch mqGeneralSwitch = new MqGeneralSwitch();
        mqGeneralSwitch.setCommandNumber(commandNumber);
        mqGeneralSwitch.setImei(imei);
        mqGeneralSwitch.setOnOff(onOff);
        byte[] messages = getMqMessage(Constants.WHITELIST_SWITCH, mqGeneralSwitch);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 获取设备步数
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/getStepNum/{imei}/{commandNumber}")
    RespCodeEntity getStepNum(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralQuery mqGeneralQuery = new MqGeneralQuery();
        mqGeneralQuery.setImei(imei);
        mqGeneralQuery.setCommandNumber(commandNumber);
        byte[] messages = getMqMessage(Constants.GET_DEVICE_STEP, mqGeneralQuery);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 修改设备M1卡号
     *
     * @param imei
     * @param commandNumber
     * @param m1Card
     * @return
     */
    @PostMapping("/updateM1Card/{imei}/{commandNumber}/{m1Card}")
    RespCodeEntity updateM1Card(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("m1Card") String m1Card) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setValue(m1Card);
        byte[] messages = getMqMessage(Constants.RESET_M1_CARD, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 清除SIM卡
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/cleanSim/{imei}/{commandNumber}")
    RespCodeEntity cleanSim(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSysSet mqGeneralSysSet = new MqGeneralSysSet();
        mqGeneralSysSet.setCommandNumber(commandNumber);
        mqGeneralSysSet.setImei(imei);
        byte[] messages = getMqMessage(Constants.REMOVE_BINDING, mqGeneralSysSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 回复未定位心跳
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/replyKA/{imei}/{commandNumber}")
    RespCodeEntity replyKA(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralReply mqGeneralReply = new MqGeneralReply();
        mqGeneralReply.setImei(imei);
        mqGeneralReply.setCommandNumber(commandNumber);
        mqGeneralReply.setResult(null);
        byte[] messages = getMqMessage(Constants.NOT_LOCATE_HEART, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 回复Weather
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/replyWeather/{imei}/{commandNumber}")
    RespCodeEntity replyWeather(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralReply mqGeneralReply = new MqGeneralReply();
        mqGeneralReply.setImei(imei);
        mqGeneralReply.setCommandNumber(commandNumber);
        mqGeneralReply.setResult("2,19,1,0");
        byte[] messages = getMqMessage(Constants.WEATHER, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 回复SOS
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/replySOS/{imei}/{commandNumber}")
    RespCodeEntity replySOS(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralReply mqGeneralReply = new MqGeneralReply();
        mqGeneralReply.setImei(imei);
        mqGeneralReply.setCommandNumber(commandNumber);
        mqGeneralReply.setResult(null);
        byte[] messages = getMqMessage(Constants.SOS_WARNING, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 回复低电
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/replyBLOW/{imei}/{commandNumber}")
    RespCodeEntity replyBLOW(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralReply mqGeneralReply = new MqGeneralReply();
        mqGeneralReply.setImei(imei);
        mqGeneralReply.setCommandNumber(commandNumber);
        mqGeneralReply.setResult(null);
        byte[] messages = getMqMessage(Constants.LOW_BATTERY_WARNING, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 回复通话记录
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/replyCallNote/{imei}/{commandNumber}")
    RespCodeEntity replyCallNote(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralReply mqGeneralReply = new MqGeneralReply();
        mqGeneralReply.setImei(imei);
        mqGeneralReply.setCommandNumber(commandNumber);
        mqGeneralReply.setResult("CALLNOTE_GET");
        byte[] messages = getMqMessage(Constants.CALL_RECORDS, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 回复监听记录
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/replyMonitorNote/{imei}/{commandNumber}")
    RespCodeEntity replyMonitorNote(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralReply mqGeneralReply = new MqGeneralReply();
        mqGeneralReply.setImei(imei);
        mqGeneralReply.setCommandNumber(commandNumber);
        mqGeneralReply.setResult("MONITORNOTE_GET");
        byte[] messages = getMqMessage(Constants.MONITOR_RECORDS, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 回复步数上报
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/replyStep/{imei}/{commandNumber}")
    RespCodeEntity replyStep(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralReply mqGeneralReply = new MqGeneralReply();
        mqGeneralReply.setImei(imei);
        mqGeneralReply.setCommandNumber(commandNumber);
        mqGeneralReply.setResult("STEP_GET");
        byte[] messages = getMqMessage(Constants.ACTIVE_UPLOAD_STEP, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置静默关机
     *
     * @param imei
     * @param commandNumber
     * @return
     */
    @PostMapping("/setSilentShutDown/{imei}/{commandNumber}/{flag}")
    RespCodeEntity setSilentShutDown(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("flag") Integer flag) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setValue(flag.toString());
        byte[] messages = getMqMessage(Constants.SHUT_DOWN_SILENT, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 设置设备闹钟
     *
     * @param data
     * @return
     */
    @PostMapping("/setAlarm")
    RespCodeEntity setAlarm(@RequestBody DeviceAlarm data) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setImei(data.getImei());
        mqGeneralSet.setCommandNumber(data.getCommandNumber());
        mqGeneralSet.setValue(data);
        byte[] messages = getMqMessage(Constants.ALARM_CLOCK, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 删除设备闹钟
     *
     * @param imei
     * @param commandNumber
     * @param sequence
     * @return
     */
    @PostMapping("/deleteAlarm/{imei}/{commandNumber}/{sequence}")
    RespCodeEntity deleteAlarm(@PathVariable("imei") String imei, @PathVariable("commandNumber") String commandNumber, @PathVariable("sequence") String sequence) {
        RespCode respCode = RespCode.SUCCESS;
        MqGeneralSet mqGeneralSet = new MqGeneralSet();
        mqGeneralSet.setImei(imei);
        mqGeneralSet.setCommandNumber(commandNumber);
        mqGeneralSet.setValue(sequence);
        byte[] messages = getMqMessage(Constants.DELETE_CLOCK, mqGeneralSet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), messages);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 下发绑定状态信息
     *
     * @param data
     * @return
     */
    @PostMapping("/setBindingStatus")
    RespCodeEntity setBindingStatus(@RequestBody DeviceBindingStatus data) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.DEVICE_BINDING_STATUS, data);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 下发文本
     *
     * @param data
     * @return
     */
    @PostMapping("/sendText")
    RespCodeEntity sendText(@RequestBody MqTextData data) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.SEND_TEXT, data);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 考勤机时间校准指令下发
     *
     * @param proNo
     * @return
     */
    @PostMapping("/sendTimeAdjusting/{proNo}")
    RespCodeEntity sendTimeAdjusting(@PathVariable("proNo") String proNo) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.TIME_CHECK, proNo);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 考勤机打卡信息上报的回复
     *
     * @param serialNo
     * @return
     */
    @PostMapping("/sendDataReply/{serialNo}/{productNo}")
    RespCodeEntity sendDataReply(@PathVariable("serialNo") String serialNo, @PathVariable("productNo") String productNo) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.SEND_DATA, serialNo.concat(",").concat(productNo));
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }


    /**
     * 关闭通道
     *
     * @param imei
     * @return
     */
    @PostMapping("/closeChannel/{imei}")
    RespCodeEntity closeChannel(@PathVariable("imei") String imei) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.CLOSE_CHANNEL, imei);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 威科姆免打扰
     *
     * @param mqVcomNotDisturb
     * @return
     */
    @PostMapping("/vcomNotDisturb")
    RespCodeEntity vcomNotDisturb(@RequestBody MqVcomNotDisturb mqVcomNotDisturb) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.VCOM_NOT_DISTURB, mqVcomNotDisturb);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 威科姆通知信息
     *
     * @param mqVcomMessage
     * @return
     */
    @PostMapping("/vcomNoticeMessage")
    RespCodeEntity vcomNoticeMessage(@RequestBody MqVcomMessage mqVcomMessage) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.VCOM_NOTICE_MESSAGE, mqVcomMessage);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * 威科姆短消息
     * 注意：对象必须实现序列化接口，否则这里拿到的就是一个Map
     *
     * @param mqVcomMessage
     * @return
     */
    @PostMapping("/vcomShortMessage")
    RespCodeEntity vcomShortMessage(@RequestBody MqVcomMessage mqVcomMessage) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.VCOM_SHORT_MESSAGE, mqVcomMessage);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    @PostMapping("/replyGetPhone")
    RespCodeEntity replyGetPhone(@RequestBody MqPhoneDatas phoneDatas) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.GET_PHONE, phoneDatas);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }


    @PostMapping("/replyUploadApp")
    RespCodeEntity replyUploadApp(@RequestBody MqGeneralReply mqGeneralReply) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.UPLOAD_APP, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }


    @PostMapping("/replyUploadAppFile")
    RespCodeEntity replyUploadAppFile(@RequestBody MqGeneralReply mqGeneralReply) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.UPLOAD_APP_FILE, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }



    /**
     * 获取MQ发送消息
     *
     * @param type
     * @param object
     * @return
     */
    public byte[] getMqMessage(String type, Object object) {
        MqMessage mqMessage = new MqMessage();
        mqMessage.setType(type);
        mqMessage.setObject(object);
        byte[] msg = ByteObjectUtils.toByteArray(mqMessage);
        return msg;
    }


    /**
     * 发送MQMsg
     *
     * @param topic
     * @param tag
     * @param msg
     * @throws RemotingException
     * @throws MQClientException
     * @throws InterruptedException
     */
    private void sendMQMsg(String topic, String tag, byte[] msg) throws RemotingException, MQClientException, InterruptedException {
        Message message = new Message(topic, tag, msg);
        // 这里用到了这个mq的异步处理，类似ajax，可以得到发送到mq的情况，并做相应的处理
        // 不过要注意的是这个是异步的
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                logger.info("传输成功," + JSON.toJSONString(sendResult));
            }

            @Override
            public void onException(Throwable e) {
                logger.error("传输失败", e);
            }
        });
    }


    /**
     * 远程调用netty删除
     *
     * @param mqDeleteCommand
     * @return
     */
    @PostMapping("/deleteCommandIn")
    public RespCodeEntity deleteCommandInRedis(@RequestBody MqDeleteRedisCommand mqDeleteRedisCommand) {
        byte[] mqMessage = getMqMessage(Constants.DELETE_REDIS_COMMAND, mqDeleteRedisCommand);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(RespCode.SUCCESS);
    }


    //获取设备鉴权密匙
    @PostMapping("/replyGetLtAuthKey")
    public RespCodeEntity replyGetLtAuthKey(@RequestBody MqGeneralReply mqGeneralReply) {
        Map map = (Map) mqGeneralReply.getResult();
        String concat = ((String) map.get("productKey")).concat(",").concat(((String) map.get("deviceKey")).concat(",").concat(((String) map.get("deviceSecret"))));
        mqGeneralReply.setResult(concat);
        byte[] mqMessage = getMqMessage(Constants.GET_LT_AUTH_KEY, mqGeneralReply);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(RespCode.SUCCESS);
    }

    /**
     * app应用市场安装开关
     *
     * @param mqAppInstallationSwitch
     * @return
     */
    @PostMapping("/appInstallationSwitch")
    public RespCodeEntity appInstallationSwitch(@RequestBody MqAppInstallationSwitch mqAppInstallationSwitch) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.APP_INSTALLATION, mqAppInstallationSwitch);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    /**
     * app禁用
     *
     * @param mqAppDisable
     * @return
     */
    @PostMapping("/appDisable")
    public RespCodeEntity appDisable(@RequestBody MqAppDisable mqAppDisable) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.APP_DISABLE, mqAppDisable);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }


    @PostMapping("/setLockNet")
    public RespCodeEntity setLockNet(@RequestBody MqLockNet mqLockNet) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.LOCK_NET, mqLockNet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    @PostMapping("/setLockCard")
    public RespCodeEntity setLockCard(@RequestBody MqLockCard mqLockCard) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.LOCK_CARD, mqLockCard);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    @PostMapping("/setLockDevice")
    public RespCodeEntity setLockDevice(@RequestBody MqLockDevice mqLockDevice) {
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.LOCK_DEVICE, mqLockDevice);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    @PostMapping("/getLockNet")
    public RespCodeEntity getLockNet(@RequestBody MqGetLockNet mqGetLockNet){
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.GET_LOCK_NET, mqGetLockNet);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    @PostMapping("/getLockCard")
    public RespCodeEntity getLockCard(@RequestBody MqGetLockCard mqGetLockCard){
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.GET_LOCK_CARD, mqGetLockCard);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }

    @PostMapping("/getLockDevice")
    public RespCodeEntity getLockDevice(@RequestBody MqGetLockDevice mqGetLockDevice){
        RespCode respCode = RespCode.SUCCESS;
        byte[] mqMessage = getMqMessage(Constants.GET_LOCK_DEVICE, mqGetLockDevice);
        try {
            sendMQMsg(producerConfig.getDeviceTopic(), producerConfig.getDeviceTag(), mqMessage);
        } catch (RemotingException | MQClientException | InterruptedException e) {
            e.printStackTrace();
        }
        return new RespCodeEntity(respCode);
    }


}
